import 'package:get/get.dart';
import 'package:largefriends/config/lf_route.dart';
import 'package:largefriends/res/lf_string_ext.dart';
import 'package:largefriends/service/lf_auth_service.dart';
import 'package:largefriends/widgets/preload_page_view.dart';

import '../../entity/entities/chat_new_message_entity.dart';

_toImageGallery({
  required String uid,
  required List<String> urls,
  int index = 0,
}) {
  Get.toNamed(LFRoute.pathImageGallery, arguments: {
    'uid': uid,
    'urls': urls,
    'index': index,
  });
}

toImageGalleryByMessage({required ChatNewMessageEntity entity}) {
  String uid = entity.sender?.profile?.userId ?? "";
  if (uid.isEmpty) {
    return;
  }
  List<String> urls = List.empty(growable: true);
  if (entity.url.isNullOrEmpty == false) {
    urls.add(entity.url!);
  } else if (entity.localImagePath.isNullOrEmpty == false) {
    urls.add(entity.url!);
  }
  if (urls.isEmpty) {
    return;
  }
  _toImageGallery(uid: uid, urls: urls, index: 0);
}

class LfImageGalleryLogic extends GetxController {
  RxInt currentIndex = 0.obs;
  late String uid;
  late List<String> urls;
  late PreloadPageController controller;
  LfAuthService authService = Get.find();

  @override
  void onInit() {
    super.onInit();
    uid = Get.arguments['uid'];
    urls = Get.arguments['urls'];
    currentIndex.value = Get.arguments['index'];
    controller = PreloadPageController(initialPage: currentIndex.value);
  }

  get isSelf => authService.currentAccount!.userId == uid;

  @override
  void onClose() {
    controller.dispose();
    super.onClose();
  }
}
